home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / surfmodl / surfm203.arc / SURFSRC.ARC / SURFBGI.PAS < prev    next >
Pascal/Delphi Source File  |  1988-01-31  |  16KB  |  549 lines

  1. {$I defines.inc}
  2. {*********************************************************}
  3. {                                                         }
  4. {       Extensions to the                                 }
  5. {       Turbo Pascal Version 4.0                          }
  6. {       GRAPH Unit Interface                              }
  7. {                                                         }
  8. {*********************************************************}
  9.  
  10. { Version 1.00a,  Date 30 Jan 1988, Author: Kevin Lowey                    }
  11.  
  12. { This unit should be included AFTER the GRAPHICS unit in your program.  It}
  13. { it implements the BGI routines for unsupported devices.  The difference  }
  14. { is these routines are always resident, instead of in a file on the disk  }
  15. { so they eat up memory.                                                   }
  16.  
  17. { In addition to this file, you will also have to change DEFINES.INC.      }
  18. { You must create a DEFINE statement which describes your device, so people}
  19. { can turn off or on the device when they compile surfmodl.                }
  20.  
  21. { To use this system, include the routines for your graphics device into   }
  22. { the case statement in each  procedure, with an IFDEF and ENDIF around it }
  23. { so people can turn off the device.  The important routines are the ones  }
  24. { to select the driver (initgraph and detectgraph), the ones to leave graph}
  25. { mode (restorecrtmode and closegraph), and the one to draw a point on the }
  26. { screen (putpixel).  Other routines such as putimage and getimage can be  }
  27. { replaced by dummy routines that do nothing.  This will get rid of some   }
  28. { functions, but the basic SURFMODL program will still work.               }
  29. {                                                                          }
  30. { See the Turbo Pascal manual (or the online help) for a description of    }
  31. { each procedure.                                                          }
  32.  
  33. { You will also have to change the definitions in the SURFGRAF unit.  This }
  34. { unit contains all the SURFMODL graphics primitives.  You will have to    }
  35. { change MAXSYS, and add your device to the list of supported devices.     }
  36. { See the VAXMATE example below and in SURFBGI for more details            }
  37.  
  38. { I give an example using the DEC VAXMATE computer.  This computer provides}
  39. { the standard CGA graphics modes, plus a 640x400x2 graphics mode which is }
  40. { almost compatible with the AT&T 640x400x2 graphics mode.  The only       }
  41. { difference is the interrupt $10 function used to enter graphics mode.    }
  42. { The memory map, etc. is the same.  I cheated in implementing the graphics}
  43. { by having the program use the AT&T .BGI driver, and give the VAXMATE     }
  44. { interrupt immediately after using INITGRAPH or SETGRAPHMODE.  The program}
  45. { thinks it is using an AT&T, so all the graphics funtions are available.  }
  46. { You may wish to use a similar trick with your driver.                    }
  47. {                                                                          }
  48. { NOTE, the vaxmate driver works as is, but should not have to be used.    }
  49. { the surfmodl disk includes a TSR program called EMULATT which makes a    }
  50. { vaxmate think it is an AT&T computer.  Once EMULATT is loaded, you can   }
  51. { choose the AT&T option from the menu and the graphics routines will work }
  52. { on the vaxmate.  I left the routines in here just for demonstration use  }
  53.  
  54. { You will also have to modify the SURFGRAF unit to include your device in }
  55. { the menu of options available.                                           }
  56.  
  57. Unit SURFBGI;  { Surfmodl BGI emulation and true BGI routines }
  58.  
  59. interface
  60. uses DOS, GRAPH;
  61.  
  62. {$IFDEF USE8087}
  63. TYPE REAL = single;
  64. {$ENDIF}
  65.  
  66. const
  67.   {Redo Turbo Pascal constants}
  68.   NOTput  = 4;
  69.   NormalPut = 0;
  70.  
  71.   GRok = 0;
  72.  
  73.   DETECT = 0;
  74.   CGA = 1;
  75.   MCGA = 2;
  76.   EGA = 3;
  77.   EGA64 = 4;
  78.   EGAMONO = 5;
  79.   RESERVED = 6;
  80.   HERCMONO = 7;
  81.   ATT400 = 8;
  82.   VGA = 9;
  83.   PC3270 = 10;
  84.  
  85.   { add your own constant here for your machine, such as SANYO = 11  }
  86.  
  87. {$IFDEF VAXMATE} {should never need to define, see notes above}
  88.   VM400 = 11;  {Vaxmate constant}
  89.   { Also add constants for all the modes available, starting at 0.   }
  90.   { See the Graph Unit description in the Turbo Pascal manual or in  }
  91.   { the online help for examples }
  92.  
  93.   { Vaxmate CGA compatible modes }
  94.   VM400C0   = CGAC0; { 320x200 palette 0: LightGreen, LightRed, Yellow; 1 page }
  95.   VM400C1   = CGAC1; { 320x200 palette 1: LightCyan, LightMagenta, White; 1 page }
  96.   VM400C2   = CGAC2; { 320x200 palette 2: Green, Red, Brown; 1 page }
  97.   VM400C3   = CGAC3; { 320x200 palette 3: Cyan, Magenta, LightGray; 1 page }
  98.   VM400Med  = 4;  { 640x200 1 page }
  99.  
  100.   { Vaxmate high resolution modes }
  101.   VM400HiCo = 5;  { 640x400 4 color 1 page }
  102.   VM400Hi   = 6;  { 640x400 1 color 1 page }
  103. {$ENDIF}
  104.  
  105.  
  106.  
  107. {  detection, initialization and crt mode routines }
  108. {  see the Turbo Pascal manual for details on what the routines must do }
  109.  
  110. procedure DetectGraph(var GraphDriver, GraphMode : integer);
  111. procedure InitGraph(var GraphDriver : integer;
  112.                     var GraphMode   : integer;
  113.                         PathToDriver : String);
  114. procedure GetModeRange(GraphDriver : integer; var LoMode, HiMode : integer);
  115. procedure SetGraphMode(Mode : integer);
  116. procedure settextjustify (Horiz, Vert : word);
  117. function getmaxcolor:word;
  118. procedure RestoreCrtMode;
  119. procedure closegraph;
  120. procedure outtextxy (x,y :integer; textline:string);
  121. function  GetMaxX : integer;
  122. function  GetMaxY : integer;
  123. procedure PutPixel(X, Y : integer; Pixel : word);
  124. function graphresult : integer;
  125. procedure GetImage(x1, y1, x2, y2 : integer;
  126.                    var BitMap);
  127. procedure PutImage(X, Y : integer; var
  128.           BitMap; BitBlt : word);
  129. function ImageSize(x1, y1, x2, y2 : integer) : word;
  130. function GraphErrorMsg(ErrorCode : integer) : string;
  131. Procedure Setcolor(color:word);
  132.  
  133.  
  134.  
  135. IMPLEMENTATION
  136.  
  137. const
  138.   { used in detect graph }
  139.   CASSETTE_IO     = $15;
  140.   DEC_CONF_WORD   = $D0;
  141.  
  142.  
  143.   {Used in setting video mode}
  144.   VIDEO_SERVICES  = $10;
  145.   DEC_HIRES       = $D0;
  146.   DEC_HIRES_COLOR = $D1;
  147.   SET_VIDEO_MODE  = $00;
  148.   write_pixel     = $0C;
  149.  
  150.   {Dummy used in CASE statements to achieve device independance}
  151.   dummy = maxint;
  152.  
  153. var
  154.   regs : registers;
  155.   grdriver : integer;
  156.   GetGraphMode : integer;
  157.   GraphError : integer;
  158.   grmaxx  : integer;
  159.   grmaxy  : integer;
  160.  
  161.  
  162. function GraphErrorMsg(ErrorCode : integer) : string;
  163. begin
  164.   GraphErrorMsg := graph.grapherrormsg(errorcode);
  165. end;
  166.  
  167.  
  168. Procedure Setcolor(color:word);
  169. begin
  170.   graph.setcolor(color);
  171. end;
  172.  
  173.  
  174. procedure outtextxy (x,y :integer; textline:string);
  175. begin
  176.   case grdriver of
  177.     dummy : ;
  178. {$IFDEF VAXMATE}
  179.     vm400 : if getgraphmode <> VM400HICO then
  180.                graph.outtextxy (x,y,textline);
  181. {$ENDIF}
  182.     else
  183.      graph.outtextxy (x,y,textline);
  184.   end; {case}
  185. end;
  186.  
  187.  
  188.  
  189. function getmaxcolor:word;
  190. begin
  191.   case grdriver of
  192.     dummy : ;
  193. {$IFDEF VAXMATE}
  194.     VM400 : begin
  195.               if (getgraphmode in [VM400c0..VM400MED,VM400HI]) then
  196.                 getmaxcolor := graph.getmaxcolor
  197.               else if getgraphmode = VM400HICO then
  198.                 getmaxcolor := 3;
  199.             end; {Vaxmate }
  200. {$ENDIF}
  201.     else begin
  202.       getmaxcolor := graph.getmaxcolor;
  203.     end;
  204.   end; {case}
  205. end; { getmaxcolor}
  206.  
  207.  
  208.  
  209. procedure PutPixel(X, Y : integer; pixel : word);
  210. begin
  211.   case GRdriver of
  212.     dummy : ;
  213. {$ifdef VAXMATE}
  214.     VM400 : begin  { Use DOS bios calls to write a pixel }
  215.               with regs do begin
  216.                 ah := write_pixel;
  217.                 al := pixel;
  218.                 cx := x;
  219.                 dx := y;
  220.               end; {with}
  221.               intr (video_services,regs);
  222.             end;
  223. {$ENDIF}
  224.     else begin {BGI}
  225.       graph.putpixel (x,y,pixel);
  226.     end; {else}
  227.   end; {case}
  228. end; {putpixel}
  229.  
  230.  
  231.  
  232.  
  233. procedure DOS_SET_MODE (MODE:BYTE);
  234. begin
  235.   {DEC specific modes}
  236.   with regs do begin
  237.     ah := Set_Video_Mode;
  238.     al := mode;
  239.   end;
  240.    intr (Video_services,regs);
  241. end; {Dos_Set_Mode}
  242.  
  243.  
  244.  
  245. procedure restorecrtmode;
  246. begin
  247.   case grdriver of
  248.     Dummy : ;
  249.     else begin
  250.        graph.restorecrtmode;
  251.     end;
  252.   end;
  253. end; {restorecrtmode}
  254.  
  255.  
  256.  
  257. procedure closegraph;
  258. begin
  259.   case grdriver of
  260.     dummy : ;
  261.     else begin
  262.       graph.closegraph;
  263.       GraphError := graph.graphresult;
  264.       grdriver := 0;
  265.     end; {else}
  266.   end; {case}
  267. end; {closegraph}
  268.  
  269.  
  270. function  GetMaxX : integer;
  271. begin
  272.   case grdriver of
  273.     dummy : ;
  274.  
  275. {$IFDEF VAXMATE}
  276.     VM400 : begin
  277.               if getgraphmode in [vm400c0 .. VM400C3] then
  278.                 getmaxx := 319
  279.               else
  280.                 getmaxx := 639;
  281.             end;
  282. {$ENDIF}
  283.  
  284.     else begin
  285.       getmaxx := graph.getmaxx;
  286.     end;
  287.   end; {case}
  288. end; {getMaxX}
  289.  
  290.  
  291.  
  292. function  GetMaxY : integer;
  293. begin
  294.   case grdriver of
  295.     dummy :;
  296.  
  297. {$IFDEF VAXMATE}
  298.     VM400 : begin
  299.               if getgraphmode in [vm400c0 .. VM400MED] then
  300.                 getmaxy := 199
  301.               else
  302.                 getmaxy := 399;
  303.             end;
  304. {$ENDIF}
  305.  
  306.     else begin
  307.       getmaxy := graph.getmaxy;
  308.     end;
  309.   end; {case}
  310. end; {getMaxy}
  311.  
  312.  
  313. function graphresult : integer;
  314. { my definition of graphresult so my routines can return proper values }
  315. begin
  316.   if (GraphError < -15) or (GraphError > 0) then
  317.     GraphError := 0;
  318.  
  319.   graphresult := GraphError;
  320.   GraphError := 0;
  321. end;
  322.  
  323.  
  324.  
  325. { *** detection, initialization and crt mode routines *** }
  326.  
  327. procedure DetectGraph(var GraphDriver, GraphMode : integer);
  328.  
  329. { this procedure must detect what machine you are on.  Some machines  }
  330. { include routines for this, while others you may have to look at the }
  331. { copyright message for the bios.                                     }
  332.  
  333. begin
  334.   graphdriver :=  0;
  335.   graphmode   :=  0;
  336.  
  337.   graph.detectgraph(graphdriver,graphmode);
  338.  
  339. {$IFDEF VAXMATE}  {This doesn't work right.  Some non-vaxmates are reported}
  340.   if graphdriver = RESERVED then begin
  341.     {check if vaxmate}
  342.     regs.ah := DEC_CONF_WORD;
  343.     intr(CASSETTE_IO,regs);
  344.  
  345.     if (regs.AH = $86) then begin  {Vaxmate Detected}
  346.       if (regs.BX and $00E0) = $0040 then begin {vaxmate graphics system}
  347.         graphdriver := VM400;
  348.         GraphMode   := VM400HI;
  349.       end
  350.     end { If machine vaxmate }
  351.   end; {if RESERVED perhaps vaxmate}
  352. {$ENDIF}
  353.  
  354.   grdriver := graphdriver;
  355.   getGraphMode := graphmode;
  356.   GraphError := graph.graphresult;
  357. end; {Detectgraph}
  358.  
  359.  
  360. procedure SetGraphMode(Mode : integer);
  361. {Routine to enter graphics mode}
  362.  
  363. begin
  364.  
  365.   case grdriver of
  366.     dummy :;
  367.  
  368. {$IFDEF VAXMATE}
  369. {Uses the AT&T graph mode so Turbo's .BGI routines work correct,      }
  370. { then calls its own interrupt to go into graphics mode on the VAXmate}
  371.     VM400 : begin
  372.               if mode in [VM400C0..VM400MED] then begin
  373.                 graph.setgraphmode (mode);
  374.                 GraphError := graph.graphresult;
  375.               end {CGA compatible modes}
  376.               ELSE if mode = VM400HI then begin
  377.                   graph.setgraphmode (ATT400HI);
  378.                   GraphError := graph.graphresult;
  379.                   dos_set_mode (DEC_Hires)
  380.               end
  381.               else if mode = VM400HICO then begin
  382.                   graph.setgraphmode (att400hi);
  383.                   grapherror := graph.graphresult;
  384.                   dos_set_mode (DEC_HIRES_COLOR)
  385.               end
  386.               else
  387.                 GraphError := grInvalidMode;
  388.             end; {Vaxmate mode}
  389. {$ENDIF}
  390.  
  391.     else Begin {Not DEC compatible mode}
  392.       graph.setgraphmode (mode);
  393.       GraphError := graph.graphresult;
  394.     end;
  395.  
  396.   end; {case}
  397. end; {setgraphmode}
  398.  
  399.  
  400.  
  401. procedure InitGraph(var GraphDriver : integer;
  402.                     var GraphMode   : integer;
  403.                         PathToDriver : String);
  404. var
  405.   temp1,temp2 : integer;
  406.  
  407. begin
  408.   if graphdriver = 0 then
  409.     detectgraph (graphdriver,graphmode);
  410.  
  411.   grdriver := GraphDriver;
  412.   getGraphMODE := GraphMode;
  413.  
  414.   case graphdriver of
  415.     dummy :;
  416.     { usually nothing is needed here because the "drivers" are already }
  417.     { "loaded" into memory for our own devices. }
  418. {$IFDEF VAXMATE}
  419.     VM400: begin
  420.              temp1 := ATT400;
  421.              if graphmode > VM400MED then
  422.                temp2 := ATT400HI
  423.              else
  424.                temp2 := graphmode;
  425.              graph.initgraph (temp1,temp2,pathtodriver);
  426.              GraphError := graph.graphresult;
  427.              if GraphError > grok then
  428.                setgraphmode (getgraphmode);
  429.            end;
  430. {$ENDIF}
  431.     else begin {not extended BGI }
  432.       graph.initgraph (graphdriver, graphmode, pathtodriver);
  433.       GraphError := graph.graphresult;
  434.       grdriver := graphmode;
  435.     end; {else}
  436.   end; { Case }
  437.  
  438. end;
  439.  
  440.  
  441.  
  442. procedure GetModeRange(GraphDriver : integer; var LoMode, HiMode : integer);
  443. begin
  444.   case graphdriver of
  445.     dummy : ;
  446.  
  447. {$IFDEF VAXMATE}
  448.     VM400 : begin
  449.               lomode := VM400C0;
  450.               HiMode := VM400HI;
  451.             end;
  452. {$ENDIF}
  453.  
  454.     ELSE begin { Not an extended mode }
  455.       graph.getmoderange(graphdriver,lomode,himode);
  456.       GraphError := graph.graphresult;
  457.     end; {Not an extended mode}
  458.   end; {Case}
  459. end; {GetModeRange}
  460.  
  461.  
  462.  
  463. procedure settextjustify (Horiz, Vert : word);
  464. begin
  465.   case grdriver of
  466.     dummy : ;
  467.     {The vaxmate version has not been implemented for 640x400x4 mode, but  }
  468.     {the 640x400x2 mode works properlly when emulating an AT&T computer    }
  469.     else begin {normal BGI routines}
  470.       graph.settextjustify (horiz, vert);
  471.       GraphError := graph.graphresult;
  472.     end;
  473.   end; {case}
  474. end; {settextjustify}
  475.  
  476.  
  477.  
  478. procedure GetImage(x1, y1, x2, y2 : integer;
  479.                    var BitMap);
  480. BEGIN
  481.   Case grdriver of
  482.  
  483.     dummy : ; {dummy position}
  484.  
  485. {$IFDEF VAXMATE}
  486.     { AT&T getimage works for all modes but 640x400x4}
  487.     VM400 : begin {Digital Vaxmate}
  488.               if getgraphmode <> VM400HICO then
  489.                 graph.getimage(x1,y1,x2,y2,bitmap)
  490.               else
  491.                 GraphError := grerror;
  492.             end;
  493. {$ENDIF}
  494.     else  graph.getimage (x1,y1,x2,y2,bitmap); {Normal BGI driver}
  495.   end; {Case}
  496. end; {getimage}
  497.  
  498.  
  499.  
  500. procedure PutImage(X, Y : integer; var
  501.           BitMap; BitBlt : word);
  502. begin
  503.   Case grdriver of
  504.     dummy : ; {dummy position}
  505.  
  506. {$IFDEF VAXMATE}
  507.     { AT&T driver works for all modes but 640x400x4}
  508.     VM400 : begin {Digital Vaxmate}
  509.               if getgraphmode <> VM400HICO then
  510.                 graph.putimage (x,y,bitmap,bitblt)
  511.               else
  512.                 grapherror := grerror;
  513.             end;
  514. {$ENDIF}
  515.  
  516.     else  graph.putimage (x,y,bitmap,bitblt); {Normal BGI driver}
  517.   end; {Case}
  518. end; {Imagesize}
  519.  
  520.  
  521.  
  522. function ImageSize(x1, y1, x2, y2 : integer) : word;
  523. begin
  524.   Case grdriver of
  525.  
  526.     dummy : ; {dummy position}
  527.  
  528. {$IFDEF VAXMATE}
  529.     {AT&T driver works for all modes but 640x400x4}
  530.     VM400 : begin {Digital Vaxmate}
  531.               if getgraphmode <> VM400HICO then
  532.                 imagesize := graph.ImageSize (x1,y1,x2,y2)
  533.               else
  534.                 imagesize := 0;
  535.             end; { Digital Vaxmate }
  536. {$ENDIF}
  537.  
  538.     else begin  { Normal BGI driver }
  539.       imagesize := graph.ImageSize (x1,y1,x2,y2); {Normal BGI driver}
  540.     end;
  541.   end; {Case}
  542. end; {imagesize}
  543.  
  544.  
  545. begin
  546.   GraphError := 0;
  547.   grdriver := 0;
  548.   getgraphmode := 0;
  549. end.